package com.aznest.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.db.sql.Order;
import com.aznest.context.BaseContext;
import com.aznest.dto.TypeWithDateDTO;
import com.aznest.mapper.OrdersMapper;
import com.aznest.service.TypeService;
import com.aznest.enums.RoleEnum;
import com.aznest.enums.ResultCodeEnum;
import com.aznest.entity.Account;
import com.aznest.entity.Type;
import com.aznest.exception.CustomException;
import com.aznest.mapper.TypeMapper;
import com.aznest.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.List;

/**
 * 房间类别表业务处理
 **/
@Service
public class TypeServiceImpl implements TypeService {

    @Resource
    private TypeMapper typeMapper;
    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * 新增
     */
    public void add(Type type) {
//        Account currentUser = TokenUtils.getCurrentUser();
        Account currentUser = BaseContext.getCurrentUser();
        if (ObjectUtil.isNull(currentUser)) {
            throw new CustomException(ResultCodeEnum.TOKEN_CHECK_ERROR);
        }
        type.setHotelId(currentUser.getId());
        typeMapper.insert(type);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        typeMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            typeMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Type type) {
        typeMapper.updateById(type);
    }

    /**
     * 根据ID查询
     */
    public Type selectById(Integer id) {
        return typeMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Type> selectAll(Type type) {
        return typeMapper.selectAll(type);
    }

    /**
     * 分页查询
     */
    public PageInfo<Type> selectPage(Type type, Integer pageNum, Integer pageSize) {
//        Account currentUser = TokenUtils.getCurrentUser();
        Account currentUser = BaseContext.getCurrentUser();
        if (ObjectUtil.isNull(currentUser)) {
           throw new CustomException(ResultCodeEnum.TOKEN_CHECK_ERROR);
        }
        if (RoleEnum.HOTEL.name().equals(currentUser.getRole())) {
            type.setHotelId(currentUser.getId());
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Type> list = typeMapper.selectAll(type);
        return PageInfo.of(list);
    }

    @Override
    public List<Type> searchTypeWithDate(TypeWithDateDTO typeWithDateDTO) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<Type> types = selectByHotelId(typeWithDateDTO.getHotelId());
        if (!CollUtil.isEmpty(types)) {
            // 循环遍历每种类型在时间段内的房间数
            for (Type type : types) {
                String in = sdf.format(typeWithDateDTO.getInTime());
                String out = sdf.format(typeWithDateDTO.getOutTime());
                Integer typeId = type.getId();
                // 查询订单表某个时间段内中，待支付、代入驻、已入住的房间数
                Integer roomCnt = ordersMapper.selectByTypeAndTime(typeId, in, out);
                type.setNum(type.getNum()-roomCnt);
            }
            return types;
        }

        return Collections.emptyList();
    }


    public List<Type> selectByHotelId(Integer hotelId) {
        return typeMapper.selectByHotelId(hotelId);
    }
}
